package com.jushu.storbox.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.util.Xml;

import com.jushu.storbox.entities.UploadSelectItem;

/**
 * 
 * XMLHelper pare class
 * 
 */
public class XMLHelper {
	/** UploadSelectItem list instance */
	private static ArrayList<UploadSelectItem> uploadSelectItemlist = null;

	/** UploadSelectItem instance */
	private static UploadSelectItem uploadItem = null;

	/** UploadItem value */
	private static final String UPLOADITEM = "UploadItem";

	/** ID value */
	private static final String ID = "id";

	/** nameCn value */
	private static final String NAMECN = "nameCn";

	/** nameEn value */
	private static final String NAMEEN = "nameEn";

	/**
	 * @param context
	 * @param xmlPath
	 * @return
	 */
	public static ArrayList<UploadSelectItem> parseUploadItemXml(Context context) {
		uploadSelectItemlist = new ArrayList<UploadSelectItem>();
		InputStream inputStream = null;
		XmlPullParser xmlParser = Xml.newPullParser();
		try {
			inputStream = context.getAssets().open("upload_select_item.xml");
			// inputStream = context.getResources().getAssets().open(xmlPath);
			xmlParser.setInput(inputStream, "utf-8");
			int evtType = xmlParser.getEventType();
			while (evtType != XmlPullParser.END_DOCUMENT) {
				switch (evtType) {

				case XmlPullParser.START_TAG:
					String tag = xmlParser.getName();
					if (tag.equalsIgnoreCase(UPLOADITEM)) {
						uploadItem = new UploadSelectItem();
						uploadItem.setId(xmlParser.getAttributeValue(null, ID));
					} else if (uploadItem != null) {
						if (tag.equalsIgnoreCase(NAMECN)) {
							uploadItem.setItemNameCn(xmlParser.nextText());
						} else if (tag.equalsIgnoreCase(NAMEEN)) {
							uploadItem.setItemNameEn(xmlParser.nextText());
						}
					}
					break;
				case XmlPullParser.END_TAG:
					if (xmlParser.getName().equalsIgnoreCase(UPLOADITEM) && uploadItem != null) {
						uploadSelectItemlist.add(uploadItem);
						uploadItem = null;
					}
					break;
				default:
					break;
				}
				evtType = xmlParser.next();
			}
		} catch (XmlPullParserException e) {
			// Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// Auto-generated catch block
			e1.printStackTrace();
		}
		return uploadSelectItemlist;
	}
}
